﻿using System.Collections.Generic;
using HK.Core.Common;
using HK.Core.Common.Data;
using HK.Core.Common.Protocol.Data;
using HK.Core.Load.Protocol.Signal;
using HK.Core.Pool;
using HK.Core.Settings;
using HLogger = HK.Core.Logs.Logger;

namespace HK.Core.Load.Signal
{

    /// <summary>
    /// 通知信号
    /// </summary>
    public class NotifySignal : JsonData<NotifySignal>, INotifySignal
    {
        /// <summary>
        /// 构造含糊
        /// </summary>
        public NotifySignal() : this(null) {}
        
        /// <summary>
        /// 保护构造函数
        /// </summary>
        /// <param name="iKey">Key</param>
        internal NotifySignal(string iKey)
        {
            Key = iKey;
        }
        
        public long PoolId { get; set; } = -1;

        /// <summary>
        /// 信号Key
        /// <para>* 唯一表示Key</para>
        /// </summary>
        public string Key { get; protected set; } = null;

        /// <summary>
        /// 已被使用
        /// </summary>
        public bool Used { get; private set; } = false;

        /// <summary>
        /// 使用
        /// <para>* 一旦信号被人捡起，请调用该函数，以便标记当前信号被使用，在下一个循环回收时，会从队列中移除该信号</para>
        /// </summary>
        public void Use() => Used = true;

        /// <summary>
        /// 有效标志位
        /// </summary>
        public override bool Valid => !string.IsNullOrEmpty(Key);

        /// <summary>
        /// 信号数据
        /// </summary>
        public IJsonData Data { get; private set; } = null;

        /// <summary>
        /// 设定信息
        /// </summary>
        /// <param name="iKey">Key</param>
        /// <param name="iData">信号数据</param>
        public void SetInfo(string iKey, IJsonData iData = null)
        {
            Key = iKey;
            Data = iData;
        }

        /// <summary>
        /// 清空
        /// </summary>
        public override void Clear()
        {
            base.Clear();

            Key = null;
            Used = false;
            Data = null;
        }
    }

    /// <summary>
    /// 信号订阅
    /// </summary>
    public sealed class SignalSubscribe : ClassExtension, ISignalSubscribe
    {

#region Create
            public static SignalSubscribe Create()
            {
                return new SignalSubscribe();
            }
#endregion
        
        /// <summary>
        /// 保护构造函数
        /// </summary>
        private SignalSubscribe() {}
        
        private SignalNotify _subscribe = (iSignal, iKey, iData) =>
        {
#if UNITY_EDITOR
            // var descTmp = string.IsNullOrEmpty(desc) ? "-" : desc;
            // Loger.Info($"SignalSubscribe::Subscribe():{state}({target.Type}) - {target.Key}({descTmp})");
#endif
        };

        /// <summary>
        /// Tab点击事件
        /// </summary>
        public event SignalNotify Subscribe
        {
            add
            {
#if UNITY_EDITOR
                if (null != value)
                {
                    Info("Subscribe(): + {0}:{1}", value.Target.GetType().FullName, value.Method.Name);
                }
#endif
                _subscribe += value;
            }
            remove
            {

#if UNITY_EDITOR
                if (null != value)
                {
                    Info("Subscribe(): - {0}:{1}", value.Target.GetType().FullName, value.Method.Name);
                }
#endif
                if (null != _subscribe) _subscribe -= value;
            }
        }

        /// <summary>
        /// 信号通知时间
        /// </summary>
        /// <param name="iSignal">信号</param>
        /// <param name="iKey">Key</param>
        /// <param name="iData">信号数据</param>
        public void Notify(int iSignal, string iKey, IJsonData iData = null)
        {
            _subscribe.Invoke(iSignal, iKey, iData);
        }
    }

    /// <summary>
    /// 信号管线
    /// </summary>
    public static class SignalPipe
    {
        static SignalPipe()
        {
            var gameSettings = GameSettings.Instance;
            if(null == gameSettings) return;
            
            ObjectPool<NotifySignal>.Generate(100, true, 0.0f);
        }

        #region SignalSubscribe
        
        /// <summary>
        /// 信号订阅列表
        /// <para>* Key : 信号群组</para>
        /// <para>* Key : 信号群组</para>
        /// </summary>
        private static Dictionary<int, Dictionary<int, SignalSubscribe>> _subscribes = new Dictionary<int, Dictionary<int, SignalSubscribe>>();

        /// <summary>
        /// 订阅信号
        /// </summary>
        /// <param name="iGroup">信号群组</param>
        /// <param name="iSignal">信号</param>
        /// <param name="iNotify">信号订阅回调</param>
        public static void SignalSubscribe(int iGroup, int iSignal, SignalNotify iNotify)
        {
            if (!_subscribes.TryGetValue(iGroup, out var oSignals))
            {
                oSignals = new Dictionary<int, SignalSubscribe>();
            }
        }

        #endregion
        
        

        /// <summary>
        /// 推送信号
        /// </summary>
        /// <param name="iGroup">信号群组</param>
        /// <param name="iSignal">信号</param>
        /// <param name="iKey">信号Key</param>
        public static void PushSignal(int iGroup, int iSignal, string iKey, IJsonData iData = null)
        {
            
        }
    }
}